package com.glsc.ngateway.platform.service.itflow.flowstrategy.cmdb.partStandardModify.taskPostHandler;

import com.alibaba.excel.util.CollectionUtils;
import com.glsc.ngateway.common.api.flowable.dto.task.doTaskParam.cmdb.CMDBPassParamDto;
import com.glsc.ngateway.common.api.flowable.dto.task.formData.cmdb.PartStandardModifyTaskFormDto;
import com.glsc.ngateway.common.api.flowable.dto.task.formData.cmdb.domainDto.FlowFormPartStandardModifyDto;
import com.glsc.ngateway.common.api.platform.dto.user.PlatformUserDto;
import com.glsc.ngateway.common.api.platform.dto.user.UserLeadersDto;
import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbCpuStandard;
import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbDiskStandard;
import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbMemoryStandard;
import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbPartStandard;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.flow.cmdb.*;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.flow.cmdb.converter.CMDBNewCPUStandardConverter;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.flow.cmdb.converter.CMDBNewDiskStandardConverter;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.flow.cmdb.converter.CMDBNewMemoryStandardConverter;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.flow.cmdb.converter.CMDBNewPartStandardConverter;
import com.glsc.ngateway.common.base.enums.itflow.ITFlowStatus;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.flow.cmdb.*;
import com.glsc.ngateway.platform.service.UserService;
import com.glsc.ngateway.platform.service.cmdb.CmdbCpuStandardService;
import com.glsc.ngateway.platform.service.cmdb.CmdbDiskStandardService;
import com.glsc.ngateway.platform.service.cmdb.CmdbMemoryStandardService;
import com.glsc.ngateway.platform.service.cmdb.CmdbPartStandardService;
import com.glsc.ngateway.platform.service.itflow.flowstrategy.AbstractHandlerHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.glsc.ngateway.common.api.common.enums.DictConstant.CMDB_CONFIRM;

/**
 * @author zhengyr
 * @date 2023/8/4-13:49
 */
@Component
public class PSMStepHandlerHelper extends AbstractHandlerHelper {
    
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final String ADDTYPE = "0";
    private final String DELETETYPE = "1";
    @Autowired
    FlowFormPartStandardModifyRepo flowFormPartStandardModifyRepo;
    @Autowired
    FlowFormNewCPUStandardDetailRepo cpuStandardNewRepo;
    @Autowired
    FlowFormNewMemoryStandardDetailRepo memoryStandardNewRepo;
    @Autowired
    FlowFormNewDiskStandardDetailRepo diskStandardNewRepo;
    @Autowired
    FlowFormNewPartStandardDetailRepo partStandardNewRepo;

    @Autowired
    private UserService userService;

    @Autowired
    CmdbCpuStandardService cmdbCpuStandardService;
    @Autowired
    CmdbDiskStandardService cmdbDiskStandardService;
    @Autowired
    CmdbMemoryStandardService cmdbMemoryStandardService;
    @Autowired
    CmdbPartStandardService cmdbPartStandardService;

    protected void endTaskAfterHandle(CMDBPassParamDto<FlowFormPartStandardModifyDto, PartStandardModifyTaskFormDto> passParam, String operator){
        //抄送给上级领导
        PlatformUserDto createUser = userService.findByUsername(operator);
        //直属领导
        UserLeadersDto userLeadersDto = userService.getDirectLeaderAndPrimaryDepartmentLeader(createUser);
        if (CollectionUtils.isEmpty(userLeadersDto.getDirectLeaders())) {
            logger.info(String.format("CMDB配件规格变更流程%c未找到直属领导信息。发起人：%s",passParam.getProcessId(), operator));
        }else{
            PartStandardModifyTaskFormDto data = passParam.getFlowableFormData();
            this.copyPorcess(passParam.getProcessId(), operator,
                    userLeadersDto.getDirectLeaders()
                    , String.format("【抄送】-【配件规格新增/删除流程】-【%s】", data.getProcessTitle())
                    , AbstractHandlerHelper.GATEWAY_CMDB_FLOW_URL + "partStandardFlowAudit?flowProcessId=" + passParam.getProcessId()
                    , String.format("申请人：" + data.getFormMainData().getFlowCreateOperatorName() +
                            "<br/>  配件类型：" + data.getFormMainData().getPartType() +
                            "<br/>  业务类型：" + (Objects.equals(data.getFormMainData().getModifyType(), "0") ?"新增":"删除") +
                            "<br/>  请知悉。可至国联技术服务平台内查阅相关事宜。"));
        }

    }
    protected void updatePartStandard(String processId, String operator){
        FlowFormPartStandardModify formMainDo = flowFormPartStandardModifyRepo.findByProcessId(processId);
        String modifyType = formMainDo.getModifyType();
        String partType = formMainDo.getPartType();
        //更新业务表数据
        if(formMainDo.getIsClose()!=0) {
            return;
        }
        switch (partType) {
            case "cpu": {
                List<FlowFormNewCPUStandardDetail> flowDataList = cpuStandardNewRepo.findAllByprocessId(processId);
                List<CmdbCpuStandard> domainList = CMDBNewCPUStandardConverter.dto2Do(flowDataList);
                if(DELETETYPE.equals(modifyType)){
                    cmdbCpuStandardService.deleteByCodeList(flowDataList.stream().map(FlowFormNewCPUStandardDetail::getCpuStandardCode).collect(Collectors.toList()),operator);
                }else{
                    domainList.stream().forEach(h->{
                        h.setId(null);
                        cmdbCpuStandardService.addOrUpdate(h,operator,CMDB_CONFIRM);
                    });
                }

                break;
            }
            case "disk": {
                List<FlowFormNewDiskStandardDetail> flowDataList = diskStandardNewRepo.findAllByprocessId(processId);
                List<CmdbDiskStandard> domainList = CMDBNewDiskStandardConverter.dto2Do(flowDataList);
                if(DELETETYPE.equals(modifyType)){
                    cmdbDiskStandardService.deleteByCodeList(flowDataList.stream().map(FlowFormNewDiskStandardDetail::getDiskStandardCode).collect(Collectors.toList()),operator);
                }else{
                    domainList.stream().forEach(h->{
                        h.setId(null);
                        cmdbDiskStandardService.addOrUpdateDiskStandard(h,operator,CMDB_CONFIRM);
                    });
                }

                break;
            }
            case "memory": {
                List<FlowFormNewMemoryStandardDetail> flowDataList = memoryStandardNewRepo.findAllByprocessId(processId);
                List<CmdbMemoryStandard> domainList = CMDBNewMemoryStandardConverter.dto2Do(flowDataList);
                if(DELETETYPE.equals(modifyType)){
                    cmdbMemoryStandardService.deleteByCodeList(flowDataList.stream().map(FlowFormNewMemoryStandardDetail::getMemoryStandardCode).collect(Collectors.toList()),operator);
                }else{
                    domainList.stream().forEach(h->{
                        h.setId(null);
                        cmdbMemoryStandardService.addOrUpdateMemoryStandard(h,operator,CMDB_CONFIRM);
                    });
                }

                break;
            }
            case "part": {
                List<FlowFormNewPartStandardDetail> flowDataList = partStandardNewRepo.findAllByprocessId(processId);
                List<CmdbPartStandard> domainList = CMDBNewPartStandardConverter.dto2Do(flowDataList);
                if(DELETETYPE.equals(modifyType)){
                    cmdbPartStandardService.deleteByCodeList(flowDataList.stream().map(FlowFormNewPartStandardDetail::getPartStandardCode).collect(Collectors.toList()),operator);
                }else{
                    domainList.stream().forEach(h->{
                        h.setId(null);
                        cmdbPartStandardService.addOrUpdatePartStandard(h,operator,CMDB_CONFIRM);
                    });
                }
                break;
            }
            default:
        }
        //更新流程主表isClose
        formMainDo.setIsClose(1);
        formMainDo.setStatus(ITFlowStatus.END.getCode());
        flowFormPartStandardModifyRepo.save(formMainDo);
    }
}
